/**
 * 解法一：逐位比较（104ms）
 */
const hammingDistance2 = function(x, y) {
    let res = 0;
    for (let i = 0; i < 32; i++) {
        if ((x & 1) != (y & 1)) {
            res ++;
        }
        x = x >> 1;
        y = y >> 1;
    }
    return res;
};

/**
 * 解法二：异或（76ms）
 */
const hammingDistance = function(x, y) {
    let res = 0;
    let z = x ^ y;
    while (z != 0) {
        if ((z & 1) == 1) {
            res ++;
        }
        z = z >> 1;
    }
    return res;
};

const x = 1, y = 4;  // output: 2
const res = hammingDistance(x, y);
console.log(res);
